<?php
/**
 * User: Kris
 * Date: 1/17/12
 */

require_once APPPATH . 'core/controller.php';

class RatingController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function votePostUp($postId)
    {
        try {
            $User = $this->em->find('User', $this->session->userdata('userId'));
            $Post = $this->em->find('Post', $postId);

            $post_rating = new Post_Rating();

            $post_rating->setUser($User);
            $post_rating->setPost($Post);
            $post_rating->setRating(1);

            $this->em->persist($post_rating);
            $this->em->flush();
        }
        catch (PDOException $e) {
            //echo nothing
        }

        $votes = $this->em->createQuery("SELECT COUNT(pr.rating) FROM post_rating pr JOIN pr.post p WHERE p.id = :postId AND pr.rating > 0");
        $votes->setParameter('postId', $postId);
        echo $votes->getSingleScalarResult();
    }

    public function votePostDown($postId)
    {
        try {
            $User = $this->em->find('User', $this->session->userdata('userId'));
            $Post = $this->em->find('Post', $postId);
            $post_rating = new Post_Rating();
            $post_rating->setUser($User);
            $post_rating->setPost($Post);
            $post_rating->setRating(-1);

            $this->em->persist($post_rating);
            $this->em->flush();
        }
        catch (PDOException $e) {
            //echo nothing
        }

        $votes = $this->em->createQuery("SELECT COUNT(pr.rating) FROM post_rating pr JOIN pr.post p WHERE p.id = :postId AND pr.rating < 0");

        $votes->setParameter('postId', $postId);
        echo $votes->getSingleScalarResult();
    }

    public function voteCommentUp($commentId)
    {
        try {
            $User = $this->em->find('User', $this->session->userdata('userId'));
            $Comment = $this->em->find('Comment', $commentId);

            $comment_rating = new Comment_Rating();
            $comment_rating->setUser($User);
            $comment_rating->setComment($Comment);
            $comment_rating->setRating(1);

            $this->em->persist($comment_rating);
            $this->em->flush();
        }
        catch (Exception $e) {
             //echo nothing
        }

        $votes = $this->em->createQuery("SELECT COUNT(cr.rating) FROM comment_rating cr JOIN cr.comment c WHERE c.id = :commentId AND cr.rating > 0");
        $votes->setParameter('commentId', $commentId);
        echo $votes->getSingleScalarResult();
    }

    public function voteCommentDown($commentId)
    {
        try {
            $User = $this->em->find('User', $this->session->userdata('userId'));
            $Comment = $this->em->find('Comment', $commentId);

            $comment_rating = new Comment_Rating();
            $comment_rating->setUser($User);
            $comment_rating->setComment($Comment);
            $comment_rating->setRating(-1);

            $this->em->persist($comment_rating);
            $this->em->flush();
        }
        catch (Exception $e) {
            //echo nothing
        }

        $votes = $this->em->createQuery("SELECT COUNT(cr.rating) FROM comment_rating cr JOIN cr.comment c WHERE c.id = :commentId AND cr.rating < 0");
        $votes->setParameter('commentId', $commentId);
        echo $votes->getSingleScalarResult();
    }
}
?>